<template>
  <Dialog
    :model-value="show"
    title="扫码预览"
    :width="500"
    @close="emits('update:show', false)"
  >
    <div class="qrcode-container">
      <qrcode-vue
        :value="content"
        :size="200"
        level="H"
      />
      <div class="tips">
        <div>手机扫左边二维码预览</div>
        <div>
          你也可以打开
          <a
            :href="content"
            target="_blank"
          >新窗口预览</a>
        </div>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <!-- <el-button
          v-clipboard="`${link}`"
          v-clipboard:success="clipboardSuccess"
          >复制链接</el-button
        > -->
        <el-button @click="copyItemUrl">复制链接</el-button>
        <el-button
          type="primary"
          @click="emits('update:show', false)"
        >关闭</el-button>
      </span>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import QrcodeVue from 'qrcode.vue';
const props = defineProps<{
  show: boolean;
  content: string;
}>()

const emits = defineEmits<{
  (e:"update:show", value: boolean):void
}>()


const copyItemUrl = () => {
  let oInput = document.createElement("input");
  // 将想要复制的值
  oInput.value = props.content;
  // 页面底部追加输入框
  document.body.appendChild(oInput);
  // 选中输入框
  oInput.select();
  // 执行浏览器复制命令
  document.execCommand("Copy");
  // 复制后移除输入框
  ElMessage({
    showClose: true,
    message: "复制成功",
    type: "success",
  });
  oInput.remove();
};
</script>

<style scoped lang="scss">
.qrcode-container {
  display: flex;
  align-items: center;
  justify-content: center;

  .tips {
    color: #666666;
    margin-left: 20px;
    font-size: 14px;

    div {
      &+div {
        margin-top: 10px;
      }
    }

    a {
      color: var(--el-color-primary);
    }
  }
}
</style>